ThisBuild / version      := "0.1"
ThisBuild / scalaVersion := "3.3.3"
ThisBuild / name         := "tinkoff-final-work"

lazy val global = project
  .in(file("."))
  .settings(settings)
  .disablePlugins(AssemblyPlugin)
  .aggregate(
    core,
    postgres,
    eventWorker,
    storageWorker,
    backofficeWorker
  )

lazy val core = project
  .settings(
    name := "core",
    settings,
    libraryDependencies ++= commonDependencies ++ Seq(
      dependencies.zioQuill,
      dependencies.vkApi
    )
  )
  .disablePlugins(AssemblyPlugin)

lazy val postgres = project
  .settings(
    name := "postgres",
    settings,
    libraryDependencies ++= commonDependencies ++ Seq(
      dependencies.zioQuill,
      dependencies.postgreSql
    )
  )
  .dependsOn(core)

lazy val eventWorker = project
  .settings(
    name := "event-worker",
    settings,
    assemblySettings,
    libraryDependencies ++= commonDependencies ++ Seq(
      dependencies.zioHttp,
      dependencies.zioKafka
    ),
    dockerExposedPorts := Seq(9999),
    dockerBaseImage    := "openjdk:21-jdk-slim"
  )
  .dependsOn(core)
  .enablePlugins(JavaAppPackaging)
  .enablePlugins(DockerPlugin)

lazy val storageWorker = project
  .settings(
    name := "storage-worker",
    settings,
    assemblySettings,
    libraryDependencies ++= commonDependencies ++ Seq(
      dependencies.zioStreams,
      dependencies.zioKafka,
      dependencies.zioQuill,
      dependencies.postgreSql,
      dependencies.vkApi
    ),
    dockerBaseImage := "openjdk:21-jdk-slim"
  )
  .dependsOn(core, postgres)
  .enablePlugins(JavaAppPackaging)
  .enablePlugins(DockerPlugin)

lazy val backofficeWorker = project
  .settings(
    name := "backoffice-worker",
    settings,
    assemblySettings,
    libraryDependencies ++= commonDependencies ++ Seq(
      dependencies.zioQuill,
      dependencies.postgreSql,
      dependencies.zioHttp
    )
  )
  .dependsOn(core, postgres)
  .enablePlugins(JavaAppPackaging)
  .enablePlugins(DockerPlugin)

// settings

lazy val settings = commonSettings ++ scalafmtSettings

lazy val compilerOptions = Seq(
  "-unchecked",
  "-feature",
  "-language:existentials",
  "-language:higherKinds",
  "-language:implicitConversions",
  "-language:postfixOps",
  "-deprecation",
  "-encoding",
  "utf8"
)

lazy val commonSettings = Seq(
  scalacOptions ++= compilerOptions,
  resolvers ++= Seq(
    "Local Maven Repository" at "file://" + Path.userHome.absolutePath + "/.m2/repository",
    Resolver.sonatypeRepo("releases"),
    Resolver.sonatypeRepo("snapshots")
  )
)

lazy val scalafmtSettings =
  Seq(
    scalafmtOnCompile := true
  )

lazy val assemblySettings = Seq(
  assemblyJarName in assembly := name.value + ".jar",
  assemblyMergeStrategy in assembly := {
    case PathList("META-INF", xs @ _*) => MergeStrategy.discard
    case "application.conf"            => MergeStrategy.concat
    case x =>
      val oldStrategy = (assemblyMergeStrategy in assembly).value
      oldStrategy(x)
  }
)

// dependencies

lazy val dependencies =
  new {

    object Versions {
      val zioConfig  = "4.0.2"
      val zioHttp    = "3.0.0-RC7"
      val zioJson    = "0.6.2"
      val zio        = "2.1.1"
      val zioStreams = "2.0.9"
      val zioKafka   = "2.7.4"
      val logback    = "1.5.6"
      val vkApi      = "1.0.16"
      val zioQuill   = "4.8.4"
      val postgreSql = "42.3.1"
    }

    val zio               = "dev.zio"       %% "zio"                 % Versions.zio
    val zioConfig         = "dev.zio"       %% "zio-config"          % Versions.zioConfig
    val zioConfigTypesafe = "dev.zio"       %% "zio-config-typesafe" % Versions.zioConfig
    val zioConfigMagnolia = "dev.zio"       %% "zio-config-magnolia" % Versions.zioConfig
    val zioHttp           = "dev.zio"       %% "zio-http"            % Versions.zioHttp
    val zioJson           = "dev.zio"       %% "zio-json"            % Versions.zioJson
    val zioStreams        = "dev.zio"       %% "zio-streams"         % Versions.zioStreams
    val zioKafka          = "dev.zio"       %% "zio-kafka"           % Versions.zioKafka
    val logback           = "ch.qos.logback" % "logback-classic"     % Versions.logback
    val vkApi             = "com.vk.api"     % "sdk"                 % Versions.vkApi
    val zioQuill          = "io.getquill"   %% "quill-jdbc-zio"      % Versions.zioQuill
    val postgreSql        = "org.postgresql" % "postgresql"          % Versions.postgreSql

  }

lazy val commonDependencies = Seq(
  dependencies.zio,
  dependencies.zioConfig,
  dependencies.zioConfigTypesafe,
  dependencies.zioConfigMagnolia,
  dependencies.zioJson,
  dependencies.logback
)
